19. Exercise: Jackson
Exercise: Jackson
This time, you're going to try serializing UdacisearchClient
to JSON (and then deserializing it from JSON), using the Jackson JSON library.
Just like the Java Object Serialization exercise, this is supposed to be just a straightforward write-then-read to make sure you can get the serialization/deserialization to work.
Read the TODO
in Main.java
and complete it:
- Create a
com.fasterxml.jackson.databind.ObjectMapper
instance and callObjectMapper#registerModule()
to register acom.fasterxml.jackson.datatype.jsr310.JavaTimeModule
. This Jackson plugin is required to be able to serialize and deserialize the classes from thejava.time
package. - Next, call
ObjectMapper#writeValue(Files.newBufferedWriter(outputPath), client)
to serialize to JSON. - Similarly, call
ObjectMapper#readValue(...)
to read the newly created JSON file and deserialize theUdacisearchClient
object. - Add some
System.out.println
s to prove that everything was serialized and deserialized as expected, then run theMain
program:
javac Main.java
java Main client.json
All done!
TODO List
Task Feedback:
Nice work! You converted a Java class to and from the JSON data format! Working with JSON is pretty easy!
Code
If you need a code on the https://github.com/udacity.
export PATH=/data/jdk-15.0.1/bin:$PATH
export JAVA_HOME=/data/jdk-15.0.1/bin
export CLASSPATH="/home/workspace:/home/workspace/lib/*"